package com.syf.kit.soa.core.spi.nacos;

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;

import java.util.Properties;

public class NacosClient {
    private NamingService namingService;

    public NacosClient(String nacosServerAddress) {
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.SERVER_ADDR, nacosServerAddress);
        properties.setProperty(PropertyKeyConst.NAMESPACE, "public");

        try {
            this.namingService = NamingFactory.createNamingService(properties);
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
    }

    public NacosClient(String nacosServerAddress,String username,String password) {
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.SERVER_ADDR, nacosServerAddress);
        properties.setProperty(PropertyKeyConst.NAMESPACE, "public");
        properties.setProperty(PropertyKeyConst.USERNAME,username);
        properties.setProperty(PropertyKeyConst.PASSWORD,password);

        try {
            this.namingService = NamingFactory.createNamingService(properties);
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
    }

    public NamingService getNamingService() {
        return namingService;
    }
}
